//
//  FileManager.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-7.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "FileManager.h"
@interface FileManager(Private)
+(NSString *)getPluPluImagePath:(NSString *)imageName;
+(NSString *)getPluBookImagePath:(NSString *)imageName;
+(NSString *)getOEMImagePath:(NSString *)imageName;
+(NSString *)getUISkinImagePath:(NSString *)imageName;
+(NSString *)getUISkinImagePath1:(NSString *)imageName;
@end

@implementation FileManager
#pragma mark - 日志记录


#pragma mark - 文件基本操作
//拷贝文件
+ (BOOL)fileCopy:(NSString *)sName TargetName:(NSString *)tName{
    NSError *error=nil;
    BOOL isSucces=NO;
    if (![FileDefaultManager fileExistsAtPath:sName]) {
        MyNSLog(@"要拷贝的源文件不存在!!!sName=%@", sName);
        return NO;
    }
    if ([FileDefaultManager fileExistsAtPath:tName]) {
        MyNSLog(@"要拷贝的目标文件已经存在!!!马上删除之...%@", tName);
        isSucces=[self fileDelete:tName];//如果不删除，后面的拷贝要失败
    }
    
    isSucces=[FileDefaultManager copyItemAtPath:sName toPath:tName error:&error];//调用拷贝方法
    if (error != nil) {
        MyNSLog(@"拷贝文件出错:%@", [error localizedDescription]);
    }else{
        MyNSLog(@"拷贝文件成功！");
    }
    return isSucces; 
}
//删除文件和文件夹
+ (BOOL)fileDelete:(NSString *)filePath{
    NSError *error=nil;
    BOOL isSucces=NO;
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        MyNSLog(@"要删除的地址不存在!!!filePath=%@", filePath);
        return NO;
    }
    
    isSucces=[FileDefaultManager removeItemAtPath:filePath error:&error];//调用删除方法
    if (error != nil) {
        MyNSLog(@"删除出错:%@", [error localizedDescription]);
    }else{
        MyNSLog(@"删除成功！");
    }
    return isSucces; 
}

//返回目录下所有的文件路径数组
+ (NSArray *)allFilesInFolderPath:(NSString *)folderPath{
	return [FileDefaultManager contentsOfDirectoryAtPath:folderPath error:nil];
}
//删除目录下所有文件和文件夹
+ (void)filesDelete:(NSString *)folderPath{
    NSArray *files = [self allFilesInFolderPath:folderPath];
    MyNSLog(@"files count=%d", [files count]);
    for (NSString *file in files) {
        NSString *filePath = [folderPath stringByAppendingPathComponent:file];//转换成url
        BOOL isDir;//判断该路径是不是文件夹
        BOOL isExists = [FileDefaultManager fileExistsAtPath:filePath isDirectory:&isDir];//返回文件或文件夹是否存在
        if (isExists) {
            [self fileDelete:filePath];
        }
    }
}
//创建文件夹
+ (BOOL)folderCreate:(NSString *)folderPath{
    NSError *error = nil;
    BOOL isSucces=NO;
    BOOL isDir;//判断该路径是不是文件夹
    BOOL isExists = [FileDefaultManager fileExistsAtPath:folderPath isDirectory:&isDir];//返回文件或文件夹是否存在
    if (isExists && isDir) {
        MyNSLog(@"%@文件夹已经存在!不需要创建!",folderPath);
        return YES;
    }
    
    MyNSLog(@"开始创建文件夹%@",folderPath);
    isSucces=[FileDefaultManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
    if (error != nil) {
        MyNSLog(@"创建出错:%@", [error localizedDescription]);
    }else{
        MyNSLog(@"创建成功！");
    }
    return isSucces;
}
+(NSString *)getVoiceFile:(NSString *)fileName{
    NSString *filePath = [AppResourcePath stringByAppendingFormat:@"/%@/%@",VoiceFolder,fileName];
    return filePath;
}
+(NSString *)getPluPluImagePath:(NSString *)imageName{
    NSString *filePath = [DocumentPath stringByAppendingFormat:@"/%@/%@",FolderPluPluImgs,imageName];//首先找沙盒路径
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingFormat:@"/%@/%@",FolderPluPluImgs,imageName];//再找程序资源路径
    }
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingPathComponent:@"default.jpg"];//最后是默认图片
    }
    return filePath;
}
+(NSString *)getPluBookImagePath:(NSString *)imageName{
    NSString *filePath = [DocumentPath stringByAppendingFormat:@"/%@/%@",FolderPluBookImgs,imageName];//首先找沙盒路径
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingFormat:@"/%@/%@",FolderPluBookImgs,imageName];//再找程序资源路径
    }
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingPathComponent:@"default.jpg"];//最后是默认图片
    }
    return filePath;
}
+(NSString *)getOEMImagePath:(NSString *)imageName{
    NSString *filePath = [DocumentPath stringByAppendingFormat:@"/%@/%@",FolderOEMImg,imageName];//首先找沙盒路径
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingFormat:@"/%@/%@",FolderOEMImg,imageName];//再找程序资源路径
    }
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingPathComponent:@"oem.png"];//最后是默认图片
    }
    return filePath;
}
+(NSString *)getUISkinImagePath:(NSString *)imageName{
    NSString *filePath = [BoundlePath stringByAppendingPathComponent:imageName];//取程序根目录下的图片
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingPathComponent:@"default.jpg"];
    }
    return filePath;
}
+(NSString *)getUISkinImagePath1:(NSString *)imageName{
    NSString *filePath = [AppResourcePath stringByAppendingFormat:@"/%@/%@",FolderUISkinImgs,imageName]; 
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingPathComponent:@"default.jpg"];
    }
    return filePath;
}
+(UIImage *)getImage:(NSString *)imageName type:(NSInteger)imageType withRect:(CGRect)rect{
    NSString *imagePath = nil;
    if (imageType == 0) {
        imagePath = [self getPluPluImagePath:imageName];
//        return [UIImage imageWithContentsOfFile:imagePath];
//        return [self scaleAndRotateImage:imagePath withSize:rect.size];
    }else if (imageType == 1) {
        imagePath = [self getPluBookImagePath:imageName];
//        return [UIImage imageWithContentsOfFile:imagePath];
    }else if(imageType == 2){
        imagePath = [self getOEMImagePath:imageName];
        return [UIImage imageWithContentsOfFile:imagePath];
    }else if(imageType == 3){
        imagePath = [self getUISkinImagePath:imageName];
        return [UIImage imageWithContentsOfFile:imagePath];
    }
    
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    if(NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0);
    else {
        UIGraphicsBeginImageContext(rect.size);
    }
    
    //方法一
    [image drawInRect:rect];
    
//    //方法二
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextTranslateCTM(context, 0.0, rect.size.height);
//    CGContextScaleCTM(context, 1.0, -1.0);
//    CGContextSetBlendMode(context, kCGBlendModeCopy);
//    CGContextDrawImage(context, CGRectMake(0.0, 0.0, rect.size.width, rect.size.height), image.CGImage);
    
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+(UIImage*) scaleAndRotateImage:(NSString *)imagePath  withSize:(CGSize)imageSize1{
    //int kMaxResolution = 300;
    UIImage *photoimage = [UIImage imageWithContentsOfFile:imagePath];
    CGImageRef imgRef =photoimage.CGImage;
    
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
    
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    
    bounds.size.width = imageSize1.width;
    bounds.size.height = imageSize1.height;
    
    CGFloat scaleRatio = bounds.size.width / width;
    CGFloat scaleRatioheight = bounds.size.height / height;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient =photoimage.imageOrientation;
    switch(orient)
    {
            
        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;
            
        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;
            
        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;
            
        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
            
        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
            
        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
            
        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
            
        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid?image?orientation"];
            break;
    }
    
    UIGraphicsBeginImageContext(bounds.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft)
    {
        CGContextScaleCTM(context, -scaleRatio, scaleRatioheight);
        CGContextTranslateCTM(context, -height, 0);
    }
    else
    {
        CGContextScaleCTM(context, scaleRatio, -scaleRatioheight);
        CGContextTranslateCTM(context, 0, -height);
    }
    
    CGContextConcatCTM(context, transform);
    
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageCopy;
} 
+(NSString *)getGeneralImagePath:(NSString *)folder name:(NSString *)imageName{
    NSString *filePath = [DocumentPath stringByAppendingFormat:@"/%@/%@",folder,imageName];//首先找沙盒路径
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingFormat:@"/%@/%@",folder,imageName];//再找程序资源路径
    }
    if (![FileDefaultManager fileExistsAtPath:filePath]) {
        filePath = [AppResourcePath stringByAppendingPathComponent:@"default.jpg"];//最后是默认图片
    }
    return filePath;
}
@end
